﻿using Cysharp.Threading.Tasks;
using LitTools;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public partial class BattleManager
{
    public LevelController LevelController { get; private set; }
    public GameObject GridPrefab { get; private set; }
    public GameObject CharacterPrefab { get; private set; }
    public GameObject EnemyPrefab { get; private set; }

    public Grid[,] Grids;
    private const float gridSize = 1;
    private const float gridSpace = .1f;

    public List<Character> Characters;

    LevelInfo testLevel = new LevelInfo()
    {
        characters = new LevelInfo_Character[]
        {
            new LevelInfo_Character(){ position = new Vector2Int(0,0)},
            new LevelInfo_Character(){ position = new Vector2Int(1,0)},
            new LevelInfo_Character(){ position = new Vector2Int(2,0)},
        },
    };

    public async UniTaskVoid EnterLevel()
    {
        //打开Loading
        await UIManager.Instance.OpenUI<LoadingPanel>();
        //卸载资源
        UIManager.Instance.CloseUI<MainPanel>();
        //切换场景
        await SceneManager.LoadSceneAsync("scene_battle");
        //加载关卡
        await CreateLevel();
        //加载网格
        await CreateGrid();
        //加载Character
        await CreateCharacter();
        //加载敌人
        EnemyPrefab = await AssetsManager.Instance.LoadAsset<GameObject>("Prefab/Enemy/Enemy");
        stateMechine.Play<BattleState_Prepare>();
    }

    /// <summary>
    /// 创建关卡
    /// </summary>
    /// <returns></returns>
    private async UniTask CreateLevel()
    {
        var levelPrefab = await AssetsManager.Instance.LoadAsset<GameObject>("Prefab/Level/Level");
        var levelGo = GameObject.Instantiate(levelPrefab);
        LevelController = new LevelController(levelGo);
        await LevelController.Init();
    }

    /// <summary>
    /// 创建网格
    /// </summary>
    /// <returns></returns>
    private async UniTask CreateGrid()
    {
        GridPrefab = await AssetsManager.Instance.LoadAsset<GameObject>("Prefab/Map/Grid");
        Grids = new Grid[5, 5];
        for (int y = -2; y < 3; y++)
        {
            float posY = (gridSize + gridSpace) * y;
            for (int x = -2; x < 3; x++)
            {
                GameObject item = GameObject.Instantiate(GridPrefab, LevelController.Root_grid);
                item.transform.localPosition = new Vector2((gridSize + gridSpace) * x, posY);
                Grids[x + 2, y + 2] = new Grid(item, new Vector2Int(x + 2, y + 2));
            }
            await UniTask.DelayFrame(1);
        }
    }

    private async UniTask CreateCharacter()
    {
        CharacterPrefab = await AssetsManager.Instance.LoadAsset<GameObject>("Prefab/Character/Character");
        Characters = new List<Character>();
        for (int i = 0; i < testLevel.characters.Length; i++)
        {
            var characterGo = GameObject.Instantiate(CharacterPrefab, LevelController.Root_character);
            var character = new Character(characterGo);
            var position = testLevel.characters[i].position;
            character.Grid = Grids[position.x, position.y];
            character.GotoGrid();
            Characters.Add(character);
        }
    }

    public async void ExitLevel()
    {
        await UIManager.Instance.OpenUI<LoadingPanel>();

    }
}
